export const formatDataToUrl = function (data, urlP) {
  if (typeof (data) === 'string') {
    return urlP + '/' + data;
  }
  if (typeof (data) === 'object') {
    let args = '?';
    if (urlP.split('?').length > 1) {
      args = '&';
    }
    let i = 0;
    for (let key in data) {
      i++;
      if (i === Object.keys(data).length) {
        args = args + key + '=' + data[key];
      } else {
        args = args + key + '=' + data[key] + '&';
      }
    }
    return urlP + args;
  }
};

export const getRate = function (num1, num2, fixNum) {
  let rate = 0;
  if(num2!=0){
    rate = (num1/num2)*100;
    let y = String(rate).indexOf(".") + 1;
    if(y>0){
      let decimals = rate.toString().split(".")[1].length;
      if(decimals>1){
        rate = rate.toFixed(fixNum)
      }
    }
  }
  return rate;
};

export const getAppId = function () {
  let appIds = getUserInfo() ? getUserInfo().x_appIds : '';
  if (appIds) {
    return appIds.split(',')[0];
  } else {
    return '';
  }
  // return 'APP007'
};
export const getSourceCode = function () {
  // return ConstStatic.SOURCECODE + getAppId()
  let SourceCode = localStorage.getItem('SourceCode');
  return SourceCode;
};

export const getUserPhoto = function () {
  let userPhoto = getUserInfo() ? getUserInfo().x_photo : '';
  if (userPhoto) {
    return userPhoto;
  }
  return '';
};

export const getUserId = function () {
  let userId = getUserInfo() ? getUserInfo().x_userId : '';
  if (userId) {
    return userId;
  }
  return '';
};

export const getUserName = function () {
  let userName = getUserInfo() ? getUserInfo().x_realName : '';
  if (userName) {
    return userName;
  }
  return '';
};
export const getRoleType = function () {
  let roleType = getUserInfo() ? getUserInfo().roleType : '';
  if (roleType) {
    if (typeof(roleType) === 'string'){
      return Number(roleType);
    }
    return roleType;
  }
  return 1;
};
export const getRoleTypeList = function () {
  let roleTypeList = getUserInfo() ? getUserInfo().roleTypeList : '';
  if (roleTypeList) {
    return roleTypeList;
  }
  return 1;
};
export const getSubjectTeamId = function () {
  let subjectTeamId = getUserInfo() ? getUserInfo().subjectTeamId : '';
  if (subjectTeamId) {
    return subjectTeamId;
  }
  return '';
};
export const getDeptCode = function () {
  let deptCode = getUserInfo() ? getUserInfo().x_deptCode : '';
  if (deptCode) {
    return deptCode;
  }
  return '';
};
export const getDeptName = function () {
  let deptName = getUserInfo() ? getUserInfo().x_deptName : '';
  if (deptName) {
    return deptName;
  }
  return '测试学校';
};
export const getJti = function () {
  let jti = getUserInfo() ? getUserInfo().jti : '';
  if (jti) {
    return jti;
  }
  return '';
};

export const getLocalStorage = function (item) {
  let data = window.localStorage.getItem(item);
  let obj = JSON.parse(data);
  if (obj && obj.localStorageitem) {
    obj = obj.localStorageitem;
  }
  return obj;
};


export const setLocalStorage = function (itemName, item) {
  let obj;
  if (!item) {
    return;
  }
  if (typeof item === 'object') {
    obj = JSON.stringify(item); //转化为JSON字符串
  } else {
    obj = JSON.stringify({'localStorageitem': item});
  }
  window.localStorage.setItem(itemName, obj);
  return obj;
};

export const removeLocalStorage = function (itemName) {
  localStorage.removeItem(itemName);
};

export const getUrlQuery = function (variable){
  let query = window.location.search.substring(1);
  let vars = query.split('&');
  for (let i=0;i<vars.length;i++) {
    let pair = vars[i].split('=');
          if (pair[0] == variable){
            return pair[1];
          }
  }
  return (false);
};

